﻿package com.eitb.programas.streaming{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.media.*;
	import flash.utils.*;
	import fl.controls.*;
	import fl.events.*;
	import flash.net.*;
	import flash.geom.*;

	import org.openvideoplayer.net.*;
	import org.openvideoplayer.events.*;

	import com.eitb.programas.streaming.literales.*;
	import com.eitb.programas.streaming.formatos.*;
	import com.eitb.literales.*;
	import com.eitb.bitmaps.*;
	import com.eitb.programas.*;
	import com.eitb.properties.*;
	import com.eitb.rutas.*;

	public class DialogoStreaming extends Sprite implements IVistaStreaming {
		private var cStreaming:IControladorStreaming;
		// UI elements
		private var video:Video;
		private var bMaxMin:Button;
		private var volumeSlider:Slider;
		private var timeDisplay:TextField;
		private var bufferingDisplay:TextField;
		private var statusDisplay:TextField;
		private var background:Shape;

		private var logo:Bitmap;
		private var eslogan:Bitmap;

		public function DialogoStreaming():void {
		}

		public function initControladorStreaming(cStreaming:IControladorStreaming):void {
			this.cStreaming = cStreaming;
		}
		public function setVisible(mostrar:Boolean):void {
			this.visible = mostrar;
		}
		public function initStage(formato:IVideoProperties):void {
			var sp:StageProperties = formato.getStageProperties();
			this.stage.stageWidth = sp.width;
			this.stage.stageHeight = sp.height;
			this.stage.align= sp.align;
			this.stage.scaleMode=sp.scaleMode;
			this.stage.addEventListener(Event.RESIZE, this.cStreaming.onResizeEvent);
		}

		public function initVideo(formato:IVideoProperties):void {
			var vp:VideoProperties = formato.getVideoProperties();
			this.video = new Video(vp.width, vp.height);
			addChild(this.video);
			this.video.x = vp.x;
			this.video.y = vp.y;
			this.video.deblocking = vp.deblocking;
			this.video.smoothing = vp.smoothing;
			this.video.addEventListener(MouseEvent.CLICK, this.cStreaming.doMaximizeMinimize);
		}
		public function initLogo(logo:ILogoProperties):void {
			var lr:Rectangle = logo.getRectangle();
			this.logo = logo.getBitmap();
			addChild(this.logo);
			this.logo.width = lr.width;
			this.logo.height = lr.height;
			this.logo.x=lr.x;
			this.logo.y=lr.y;
		}
		public function initEslogan(eslogan:ILogoProperties):void {
			var er:Rectangle = eslogan.getRectangle();
			this.eslogan = eslogan.getBitmap();
			addChild(this.eslogan);
			this.eslogan.x = er.x;
			this.eslogan.y = er.y;
		}
		public function initBackground(formato:IVideoProperties):void {
			var bp:BackgroundProperties = formato.getBackgroundProperties();
			this.background=new Shape();
			addChild(this.background);
			this.background.graphics.beginFill(bp.color);
			this.background.graphics.drawRoundRect(bp.roundRect.x,bp.roundRect.y,bp.roundRect.width,bp.roundRect.height,bp.roundRect.ellipseWidth);
			this.background.graphics.endFill();
		}
		public function initMaximizeButton(formato:IVideoProperties, label:String):void {
			var mbp:ButtonProperties = formato.getMaximizeButtonProperties();
			this.bMaxMin=new Button();
			addChild(this.bMaxMin);
			this.bMaxMin.label=label;
			this.bMaxMin.enabled= mbp.enabled;
			this.bMaxMin.width= mbp.width;
			this.bMaxMin.height= mbp.height;
			this.bMaxMin.x= mbp.x;
			this.bMaxMin.y =mbp.y;
			this.bMaxMin.visible=mbp.visible;
			this.bMaxMin.addEventListener(MouseEvent.CLICK, this.cStreaming.doMaximizeMinimize);
		}

		public function initVolumeSlider(formato:IVideoProperties):void {
			var vsp:VolumeSliderProperties = formato.getVolumeSliderProperties();
			this.volumeSlider=new Slider();
			addChild(this.volumeSlider);
			this.volumeSlider.width=vsp.width;
			this.volumeSlider.x=vsp.x;
			this.volumeSlider.y=vsp.y;
			this.volumeSlider.minimum=vsp.minimum;
			this.volumeSlider.maximum=vsp.maximum;
			this.volumeSlider.value=vsp.value;
			this.volumeSlider.enabled=vsp.enabled;
			this.volumeSlider.addEventListener(SliderEvent.CHANGE, this.cStreaming.volumeHandler);
		}
		public function initTimeDisplay(formato:IVideoProperties):void {
			var tdp:TextDisplayProperties = formato.getTimeDisplayProperties();
			this.timeDisplay=new TextField();
			addChild(this.timeDisplay);
			this.timeDisplay.x=tdp.x;
			this.timeDisplay.y=tdp.y;
			this.timeDisplay.width=tdp.width;
			this.timeDisplay.height=tdp.height;
			this.timeDisplay.defaultTextFormat=tdp.textFormat;
		}
		public function initBufferingDisplay(formato:IVideoProperties, text:String):void {
			var bdp:TextDisplayProperties = formato.getBufferingDisplayProperties();
			this.bufferingDisplay=new TextField  ;
			addChild(this.bufferingDisplay);
			this.bufferingDisplay.x=bdp.x;
			this.bufferingDisplay.y=bdp.y;
			this.bufferingDisplay.width=bdp.width;
			this.bufferingDisplay.height=bdp.height;
			this.bufferingDisplay.defaultTextFormat=bdp.textFormat;
			this.bufferingDisplay.text=text;
		}
		public function initStatusDisplay(formato:IVideoProperties, text:String):void {
			var sdp:TextDisplayProperties = formato.getStatusDisplayProperties();
			this.statusDisplay=new TextField  ;
			addChild(this.statusDisplay);
			this.statusDisplay.x=sdp.x;
			this.statusDisplay.y=sdp.y;
			this.statusDisplay.width=sdp.width;
			this.statusDisplay.height=sdp.height;
			this.statusDisplay.defaultTextFormat=sdp.textFormat;
			this.statusDisplay.multiline=sdp.multiline;
			this.statusDisplay.wordWrap=sdp.wordWrap;
			this.statusDisplay.text=text;
		}

		// VISTA STREAMING
		public function showElements(mostrar:Boolean):void {
			if (null != this.background) {
				this.background.visible = mostrar;
			}
			if (null != this.bMaxMin) {
				this.bMaxMin.visible = mostrar;
			}
			if (null != this.volumeSlider) {
				this.volumeSlider.visible = mostrar;
			}
			if (null != this.timeDisplay) {
				this.timeDisplay.visible = mostrar;
			}
			if (null != this.bufferingDisplay) {
				this.bufferingDisplay.visible = mostrar;
			}
			if (null != this.statusDisplay) {
				this.statusDisplay.visible = mostrar;
			}
		}
		public function getDisplayState():String {
			return this.stage.displayState;
		}
		public function setDisplayState(state:String):void {
			this.stage.displayState = state;
		}
		public function getStageSize():Point {
			return new Point(this.stage.stageWidth,this.stage.stageHeight);
		}
		public function dispatchStageEvent(event:Event):Boolean {
			return this.stage.dispatchEvent(event);
		}
		public function fitScreen():void {
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		public function setVideoLocation(point:Point):void {
			this.video.x = point.x;
			this.video.y = point.y;
		}
		public function setVideoSize(point:Point):void {
			this.video.width = point.x;
			this.video.height = point.y;
		}
		public function attachVideoNetStream(ns:NetStream):void {
			this.video.attachNetStream(ns);
		}
		public function setStatusDisplayText(text:String):void {
			if (null != this.statusDisplay) {
				this.statusDisplay.text = text;
			}
		}
		public function setVolumenSliderEnabled(enabled:Boolean):void {
			if (null != this.volumeSlider) {
				this.volumeSlider.enabled = enabled;
			}
		}
		public function setTimeDisplayText(text:String):void {
			if (null != this.timeDisplay) {
				this.timeDisplay.text = text;
			}
		}
		public function setBufferDisplayVisible(visible:Boolean):void {
			if (null != this.bufferingDisplay) {
				this.bufferingDisplay.visible = visible;
			}
		}
		public function setStatusDisplayVisible(visible:Boolean):void {
			if (null != this.statusDisplay) {
				this.statusDisplay.visible = visible;
			}
		}
		
		public function setBufferDisplayText(text:String):void {
			if (null != this.bufferingDisplay) {
				this.bufferingDisplay.text = text;
			}
		}
		public function setBotonMaxMinEnabled(enabled:Boolean):void {
			if (null != this.bMaxMin) {
				this.bMaxMin.enabled = enabled;
			}
		}
		// VISTA STREAMING
	}
}